//
//  DayArrangeCell.swift
//  ImGuider X
//
//  Created by 王鹏宇 on 9/28/18.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

protocol ArrangeCellDelegate : class {
    
    func recordThePlayIndex(indexPath : IndexPath);
}
class ArrangeCell: UITableViewCell {

    @IBOutlet weak var timeLabel: UILabel!
    
    @IBOutlet weak var markIconImageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var addressLabel: UILabel!
    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!
    
    @IBOutlet weak var imageViewHeight: NSLayoutConstraint!
    
    @IBOutlet weak var srcView: UIView!
    
    @IBOutlet weak var playBtn: UIButton!
    
    private var imageHeight:CGFloat = 0
    
    var delegate : ArrangeCellDelegate?
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        self.selectionStyle = .none
        self.nameLabel.textColor = kTitleColor
        self.addressLabel.textColor = kContentColor
        self.timeLabel.textColor = kTitleColor
        
        self.markIconImageView.layer.cornerRadius = 2.5
        self.markIconImageView.clipsToBounds = true
        self.markIconImageView.backgroundColor = UIColor.hexStringToColor(hexString: "#4A4A4A")
        
        self.imageView1.layer.cornerRadius = 5
        self.imageView2.layer.cornerRadius = 5
        self.imageView1.clipsToBounds = true
        self.imageView2.clipsToBounds = true
        
        self.contentView.backgroundColor = kThemeBgColor
        
        let width = CGFloat(kScreenWidth) - 106
        
       self.imageHeight = (width - 10) / 2 * 82 / 110
 
        self.imageViewHeight.constant = self.imageHeight
        
        NotificationCenter.default.addObserver(self, selector: #selector(updatePlayState(notif:)), name: NSNotification.Name(rawValue: WPY_PlayerState), object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    var calendar:Calendar? {
        
        didSet {
            
            timeLabel.text = calendar?.planTime
            nameLabel.text = calendar?.title
            addressLabel.text = calendar?.intro
            
            let pics = calendar?.pictureArray
            let count = pics?.count ?? 0

            
            let placeholder = UIImage(named: "placeholder_rec")!
            
            if count > 1 {
                
                self.imageView1.setImage(string: pics?[0], placeholder:placeholder)
                self.imageView2.setImage(string: pics?[1], placeholder:placeholder)
                
                self.imageView1.isHidden = false
                self.imageView2.isHidden = false
                
            } else if count == 1{
                
                self.imageView1.setImage(string: pics?[0], placeholder:placeholder)
                
                self.imageView1.isHidden = false
                self.imageView2.isHidden = true
            } else {
                
                self.imageView1.isHidden = true
                self.imageView2.isHidden = true
            }
            
            if  count > 0 {
                
                self.imageViewHeight.constant = imageHeight
                self.srcView.isHidden = false
            } else {
                
                self.imageViewHeight.constant = 0
                self.srcView.isHidden = true
            }
            
            if (calendar?.viewDetail?.lines?.first) != nil {
                self.playBtn.isHidden = false
            }else{
                self.playBtn.isHidden = true
            }
        }
    }
    
    @IBAction func playAction(_ sender: UIButton) {
        
        guard let playPath = self.calendar?.viewDetail?.lines?.first?.playpath else {return}
        if playPath == WPY_AVPlayer.playManager.currentUrl {
            
            if WPY_AVPlayer.playManager.isPlay {
                WPY_AVPlayer.playManager.pause()
                
            }else{
                WPY_AVPlayer.playManager.play()
            }
            
        }else {
            WPY_AVPlayer.playManager.playMusic(url:playPath, type: WPY_AVPlayerType.PlayTypeTry)
        }
    }
    
    
    
   @objc func updatePlayState(notif : Notification) {
    
    guard let playPath = calendar?.viewDetail?.lines?.first?.playpath else{
        return
    }
    
    if let url = notif.userInfo?[CurrentPlayUrl] as? String,url != playPath {
        
        
        self.playBtn.setBackgroundImage(UIImage(named: "playblue"), for: UIControl.State.normal)
        return
    }
    
    guard let state = notif.userInfo?[WPY_PlayerState] as? AVPlayerPlayState else {return}
    
    switch state {
    case AVPlayerPlayState.AVPlayerPlayStatePreparing,AVPlayerPlayState.AVPlayerPlayStateBeigin:
        
        self.playBtn.setBackgroundImage(UIImage(named: "pauseblue"), for: UIControl.State.normal)
    case AVPlayerPlayState.AVPlayerPlayStatePlaying:
        
        self.playBtn.setBackgroundImage(UIImage(named: "pauseblue"), for: UIControl.State.normal)
    case .AVPlayerPlayStatePause:
        
        self.playBtn.setBackgroundImage(UIImage(named: "playblue"), for: UIControl.State.normal)
    case .AVPlayerPlayStateEnd:
        
        self.playBtn.setBackgroundImage(UIImage(named: "playblue"), for: UIControl.State.normal)
        
    case .AVPlayerPlayStateNotPlay:
        
        self.playBtn.setBackgroundImage(UIImage(named: "playblue"), for: UIControl.State.normal)
//    case .AVPlayerPlayStateseekToZeroBeforePlay:
        
    
    default:
        
        self.playBtn.setBackgroundImage(UIImage(named: "playblue"), for: UIControl.State.normal)
    }
    
    }
    
}
